//
//  TalkBaseView.m
//  talkDemo
//
//  Created by telen on 15/4/20.
//  Copyright (c) 2015年 刘赞黄Telen. All rights reserved.
//

#import "TalkBaseView.h"
#import "lyt.h"

@implementation TalkBaseView

- (void)show{};
- (void)unshow{};

@end



@implementation TalkBlackBg

+ (TalkBlackBg *)talkBlackBgAlinToParent:(UIView *)view
{
    TalkBlackBg* blackBg = [[TalkBlackBg alloc] init];
    [view addSubview:blackBg];
    blackBg.backgroundColor = [UIColor blackColor];
    blackBg.alpha = 0;
    blackBg.translatesAutoresizingMaskIntoConstraints = NO;
    [blackBg lyt_alignToParent];
    return blackBg;
}

- (void)show
{
    [UIView animateWithDuration:0.3f animations:^{
        self.alpha = 0.66f;
    }];
}

- (void)unshow
{
    [UIView animateWithDuration:0.3f animations:^{
        self.alpha = 0;
    }];
}

@end


@implementation TalkDoubleLabel

- (instancetype)initWithFrame:(CGRect)frame MainText:(NSString *)str1 subText:(NSString *)str2 sepRatio:(float)sep
{
    if (sep<0 || sep > 1) {
        return nil;
    }
    if (self = [super initWithFrame:frame]) {
        _label_main = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height*sep)];
        _label_main.backgroundColor = [UIColor clearColor];
        _label_main.text = str1;
        _label_main.textColor = [UIColor whiteColor];
        _label_main.font = [UIFont systemFontOfSize:_label_main.frame.size.height*0.9];
        [self addSubview:_label_main];
        //
        _label_sub = [[UILabel alloc] initWithFrame:CGRectMake(0, _label_main.frame.size.height, frame.size.width, frame.size.height*(1-sep))];
        _label_sub.backgroundColor = [UIColor clearColor];
        _label_sub.text = str2;
        _label_sub.textColor = [UIColor whiteColor];
        _label_sub.font = [UIFont systemFontOfSize:_label_sub.frame.size.height*0.9];
        [self addSubview:_label_sub];
        //
        _ratio_sep = sep;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame MainText:(NSString *)str1 subText:(NSString *)str2 sepRatio:(float)sep orderNumSign:(NSString *)sign
{
    if (sep<0 || sep > 1) {
        return nil;
    }
    if (self = [super initWithFrame:frame]) {
        _label_orderSign = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, frame.size.height*1.5f*sep, frame.size.height*sep)];
        _label_orderSign.backgroundColor = [UIColor clearColor];
        _label_orderSign.text = sign;
        _label_orderSign.textColor = [UIColor whiteColor];
        _label_orderSign.font = [UIFont systemFontOfSize:_label_orderSign.frame.size.height*0.9];
        [self addSubview:_label_orderSign];
        //
        _label_main = [[UILabel alloc] initWithFrame:CGRectMake(_label_orderSign.frame.size.width, 0, frame.size.width-_label_orderSign.frame.size.width, frame.size.height*sep)];
        _label_main.backgroundColor = [UIColor clearColor];
        _label_main.text = str1;
        _label_main.textColor = [UIColor whiteColor];
        _label_main.font = [UIFont systemFontOfSize:_label_main.frame.size.height*0.9];
        [self addSubview:_label_main];
        //
        _label_sub = [[UILabel alloc] initWithFrame:CGRectMake(_label_orderSign.frame.size.width, _label_main.frame.size.height, frame.size.width-_label_orderSign.frame.size.width, frame.size.height*(1-sep))];
        _label_sub.backgroundColor = [UIColor clearColor];
        _label_sub.text = str2;
        _label_sub.textColor = [UIColor whiteColor];
        _label_sub.font = [UIFont systemFontOfSize:_label_sub.frame.size.height*0.9];
        [self addSubview:_label_sub];
        //
        _ratio_sep = sep;
    }
    return self;
}

- (void)setTexttextAlignment:(NSTextAlignment)textAlignment
{
    _label_main.textAlignment = textAlignment;
    _label_sub.textAlignment = textAlignment;
}

@end


@implementation TalkTimeSliderView
{
    UIView* sliderView;
    NSTimeInterval sec_from;
    NSTimeInterval sec_to;
}

- (instancetype)initWithFrame:(CGRect)frame color:(UIColor *)color_back color:(UIColor *)color_fore secondForm:(NSTimeInterval)sec_f to:(NSTimeInterval)sec_t point:(NSArray *)points
{
    if (sec_f == sec_t) {
        return nil;
    }
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = color_back;
        sec_from = sec_f;
        sec_to = sec_t;
        _countdown_sec = 0.1;
        //
        CGRect fra = frame;
        fra.origin = CGPointZero;
        sliderView = [[UIView alloc] initWithFrame:fra];
        sliderView.backgroundColor = color_fore;
        
        [self addSubview:sliderView];
        
        //sign
        for (NSNumber* num in points) {
            NSTimeInterval time = [num doubleValue];
            NSTimeInterval distanceFrom = fabs(time - sec_from);
            NSTimeInterval distanceAll = fabs(sec_from-sec_to);
            NSTimeInterval distanceTo = fabs(time - sec_to);
            if (distanceFrom <= distanceAll &&  distanceTo <= distanceAll ) {
                fra = CGRectMake(frame.size.width*distanceFrom/distanceAll-1, 0, 2, frame.size.height);
                UIView * line = [[UIView alloc] initWithFrame:fra];
                line.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.66];
                [self addSubview:line];
            }
        }
        //
        self.alpha = 0;
    }
    return self;
}

- (void)show
{
    self.alpha = 0;
    CGRect frameFull = sliderView.frame;
    CGRect frameTo = sliderView.frame;
    frameFull.size.width = self.frame.size.width;
    NSTimeInterval distanceFrom = fabs(_countdown_sec - sec_from);
    NSTimeInterval distanceAll = fabs(sec_from-sec_to);
    NSTimeInterval distanceTo = fabs(_countdown_sec - sec_to);
    if (distanceFrom <= distanceAll &&  distanceTo <= distanceAll ) {
        _progress =distanceFrom/distanceAll;
    }else if (distanceFrom >= distanceAll) {
        _progress = 1;
    }else{
        _progress = 0;
    }
    frameTo.size.width = frameFull.size.width*_progress;
    sliderView.frame = frameTo;
    [UIView animateKeyframesWithDuration:0.6f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.3 animations:^{
            self.alpha = 1;
        }];
        [UIView addKeyframeWithRelativeStartTime:0.3 relativeDuration:0.7 animations:^{
            sliderView.frame = frameTo;
        }];
    } completion:^(BOOL finished) {
    }];
    
    self.isShown = YES;
}

- (void)unshow
{
    [UIView animateKeyframesWithDuration:0.3f delay:0.0f options:0 animations:^{
        [UIView addKeyframeWithRelativeStartTime:0.0 relativeDuration:0.9 animations:^{
            self.alpha = 0;
        }];
    } completion:^(BOOL finished) {
        self.countdown_sec = 0.1;
    }];
    
    self.isShown = NO;
}

- (void)setCountdown_sec:(NSTimeInterval)countdown_sec
{
    _countdown_sec = countdown_sec;
    CGRect frameFull = sliderView.frame;
    CGRect frameTo = sliderView.frame;
    frameFull.size.width = self.frame.size.width;
    NSTimeInterval distanceFrom = fabs(_countdown_sec - sec_from);
    NSTimeInterval distanceAll = fabs(sec_from-sec_to);
    NSTimeInterval distanceTo = fabs(_countdown_sec - sec_to);
    if (distanceFrom <= distanceAll &&  distanceTo <= distanceAll ) {
        _progress =distanceFrom/distanceAll;
    }else if (distanceFrom >= distanceAll) {
        _progress = 1;
    }else{
        _progress = 0;
    }
    frameTo.size.width = frameFull.size.width*_progress;
    sliderView.frame = frameTo;
}

-(void)setForeColor:(UIColor*)color
{
    sliderView.backgroundColor = color;
}
@end

